package breeze.lepei.der.view.barchartview.view;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import breeze.lepei.der.BaseView;
import breeze.lepei.der.R;
import breeze.lepei.der.lepei.LepeiApiUti;
import breeze.lepei.der.usersettings.UserSettings;
import breeze.lepei.der.utils.AppTools;
import breeze.lepei.der.utils.MLog;
import breeze.lepei.der.view.barchartview.model.BarChartViewModel;
import com.google.gson.Gson;
import java.io.IOException;
import android.widget.LinearLayout;
import android.widget.TextView;
import breeze.lepei.der.utils.MDialog;
import java.util.List;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.widget.HorizontalScrollView;

public class BarChartView extends LinearLayout{
    
    public static final String TAG = "BarChartView";
	private LinearLayout linearview;
	private HorizontalScrollView scrollView;
    
    public BarChartView(Context context){
		super(context);
	}
	
	public BarChartView(Context context,AttributeSet sets){
		super(context,sets);
	}
	
	public BarChartView(Context context,AttributeSet set,int i){
		super(context,set,i);
	}
    
	public void init(final Context context) {
		LayoutInflater.from(context).inflate(R.layout.view_barchart,this);
		linearview=findViewById(R.id.barchartview_linear);
		scrollView=findViewById(R.id.view_barchartHorizontalScrollView);
		final UserSettings sets=UserSettings.getInstance(context);
		BaseView.getExec().execute(new Runnable(){public void run() {
					String link=LepeiApiUti.getExamSummaryGraph(sets.getUserPhone(), sets.getToken(), sets.getStudentId());
					try {
						String result=AppTools.getWebContent(link);
						Gson gson=new Gson();
						final BarChartViewModel data=gson.fromJson(result,BarChartViewModel.class);
						BarChartView.this.post(new Runnable(){public void run(){
							if("200".equals(data.getCode())){
								//TODO：设定统计图
								BarChartViewModel.Obj obj=data.getObj();
								double studentScore=obj.getStuScore();
								List<BarChartViewModel.Obj.Data> datas=obj.getData();
								for(BarChartViewModel.Obj.Data data2:datas){
									View item=LayoutInflater.from(context).inflate(R.layout.summary_item,null);
									TextView score=item.findViewById(R.id.tongjituitemTextView1);
									TextView people=item.findViewById(R.id.tongjituitemTextView2);
									LinearLayout linear=item.findViewById(R.id.tongjituitemLinearLayout1);
									score.setText(String.valueOf(data2.getName()));
									people.setText(String.valueOf(data2.getCount()));
									LinearLayout.LayoutParams lp=(LayoutParams)linear.getLayoutParams();
									lp.height=data2.getCount()/5;
									linear.setLayoutParams(lp);
									if((studentScore>=data2.getName())&&(studentScore<=(data2.getName()+10))){
										linear.setBackground(new ColorDrawable(Color.RED));
										score.setTextColor(Color.RED);
										people.setTextColor(Color.RED);
									}else{
										linear.setBackground(new ColorDrawable(Color.parseColor("#009688")));
										score.setTextColor(Color.BLACK);
										people.setTextColor(Color.BLACK);
									}
									linearview.addView(item);
								}
							}else{
								MDialog.show(context,"获取统计图失败",data.getMsg());
							}
						}});
					} catch (IOException e) {
						MLog.logE(TAG, e);
					}
				}});
	}
}
